<template>
  <div class="container">
    <Row>
      <Col span="24" @click="goSearch">
        <Sticky z-index="1000">
          <Search
            disabled
            placeholder="请输入搜索关键词"
            input-align="center"
          />
        </Sticky>
      </Col>

      <Col span="4">
        <Sticky offset-top="55px">
          <Sidebar v-model="active" class="sidebar">
            <SidebarItem
              v-for="(item, index) in categoryList.categoryL1List"
              :key="item.id"
              :title="item.name"
              :class="active === index ? 'active-sidebar ' : ''"
              @click="getData(item.id)"
            />
          </Sidebar>
        </Sticky>
      </Col>

      <Col span="20" class="main-right">
        <Image
          width="100%"
          height="96px"
          class="img"
          :src="categoryList.currentCategory?.bannerList[0].picUrl"
        ></Image>
        <div
          class="cateList"
          v-for="(cgList, index) in categoryList.categoryGroupList"
          :key="cgList.id"
        >
          <div class="hd">{{ cgList.name }}</div>
          <ul class="list">
            <li
              class="cateItem"
              v-for="(cList, index) in cgList.categoryList"
              :key="cList.id"
              @click="goProdList(cList)"
            >
              <Image :src="cList.wapBannerUrl"></Image>
              <div class="name">{{ cList.name }}</div>
            </li>
          </ul>
        </div>
      </Col>
    </Row>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "Category",
});
</script>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Col, Row, Search, Sidebar, SidebarItem, Image, Sticky } from "vant";
import { reqGetCategoryList } from "../../api/category";
import { useRouter } from "vue-router";
const router = useRouter();
let active = ref(0);
const categoryList = ref([]);
// 点击搜索
const goSearch = () => router.push("/search");
// 获取内容
const getData = async (categoryId?: number) => {
  const res = await reqGetCategoryList(categoryId);
  categoryList.value = res.data;
};
// 商品列表
const goProdList = (cList: any) => {
  router.push(
    `/prodlist?categoryId=${categoryList.value.currentCategory.id}&subCategoryId=${cList.id}`
  );
};
// 挂载完毕
onMounted(async () => {
  await getData();
  getData(categoryList.value.currentCategory.id);
});
</script>

<style scoped>
.active-sidebar {
  color: red;
}
.van-sidebar-item--select:before {
  background-color: red;
}
/* .top-col {
  border-bottom: 1px solid black;
  
} */
.van-col--4 {
  max-width: 22%;
  border-right: 1px solid black;
}
.van-col--4 .sidebar {
  /* position: fixed; */
}
.van-col--20 {
  max-width: 78%;
  padding: 20px;
}
.main-right {
  /* position: relative; */
}
.main-right .cateList .hd {
  font-size: 15px;
  font-weight: bolder;
  border-bottom: 1px solid gainsboro;
  height: 20px;
  line-height: 20px;
  padding-bottom: 5px;
  margin: 20px 0;
}
.main-right .cateList .list {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
.main-right .cateList .list .cateItem {
  width: 80px;
  text-align: center;
  height: 100px;
}
.main-right .cateList .list .name {
  font-size: small;
  height: 20px;
  line-height: 20px;
}
</style>
